@echo off
%~3

set temp_path=%~1\temp

mkdir "%temp_path%\include"
mkdir "%temp_path%\output"
mkdir "%temp_path%\images"
mkdir "%temp_path%\code"

echo transform latex sources to unicode...
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.tex -recursive
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.sty -recursive
ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.cls -recursive
copy /y "%~2.bib" /b "%temp_path%\%~2.bib" /b
rem ansi2utf8 -dir -in "%~1" -out "%temp_path%" -search *.bib -recursive

call "C:\squeak\run.squeak.cmd" "C:\scripts\text2latex.script" "%~1\code\java-parser-grammar.st" "%temp_path%\code\java-parser-grammar.st.tex"
call "C:\squeak\run.squeak.cmd" "C:\scripts\text2latex.script" "%~1\code\ParsedFileExample.xml" "%temp_path%\code\ParsedFileExample.xml.tex"
call "C:\squeak\run.squeak.cmd" "C:\scripts\text2latex.script" "%~1\code\simple-text-file.xml" "%temp_path%\code\simple-text-file.xml.tex"
call "C:\squeak\run.squeak.cmd" "C:\scripts\text2latex.script" "%~1\code\xml-file.xml" "%temp_path%\code\xml-file.xml.tex"
call "C:\squeak\run.squeak.cmd" "C:\scripts\text2latex.script" "%~1\code\unified-xml-file.xml" "%temp_path%\code\unified-xml-file.xml.tex"

echo transform images with imagemagic...
for %%I in ("%~1\images\*.jpg") do convert "%%~I" "%temp_path%\images\%%~nI.png"

cd "%temp_path%"

echo RAW=====================================
echo RAW=============== /b/c2/cXETEX/b/c0/c ===============
echo RAW=====================================
xelatex "%~2" --include-directory ".\include" --output-directory ".\output"
bibtex8 -B -c cp1251.csf ".\output\%~2.aux"
ansi2utf8 -in ".\output\%~2.bbl" -overwrite
xelatex "%~2" --include-directory ".\include" --output-directory ".\output"
xelatex "%~2" --include-directory ".\include" --output-directory ".\output"
xelatex "%~2" --include-directory ".\include" --output-directory ".\output"

echo copying %~2.pdf to root directory...
copy /y .\%~2.pdf /b ..\%~2.pdf /b
cd %~1
copy /y .\%~2.pdf /b ..\%~2.pdf /b
rmdir /q /s "%temp_path%"
